﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class PriceList : UserControl
    {
        public PriceList()
        {
            InitializeComponent();
        }

        private bool IsClose = true;
        private string[] Week = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
        private string OldeWeek = string.Empty; //修改的日期
        private string chegeWeek = string.Empty;//修改后的日期

        private int _RowCount;
        private int _ColCount = 6;
        private string _RoomType;
        private List<List<string>> _Day;
        private List<List<string>> _Time;
        private string[] _Price;
        private string[] _OnLienPrice;

        [Category("ternence"), Description("生成行数")]
        public int RowCount
        {
            get { return _RowCount; }
            set
            {
                _RowCount = value;
                if (value > 0)
                    CreatTable();
                this.Invalidate();
            }
        }

        [Category("ternence"), Description("生成列数")]
        public int ColCount
        {
            get { return _ColCount; }
            set { _ColCount = value; }
        }


        [Category("ternence"), Description("包间类型")]
        public string RoomType
        {
            get { return _RoomType; }
            set
            {
                _RoomType = value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    InsertRoomType();
                }

            }
        }

        [Category("ternence"), Description("日期")]
        public List<List<string>> Day
        {
            get { return _Day; }
            set
            {
                if (_Day != value)
                    CreatSave();
                _Day = value;
                if (_Day == null) return;
                if (_Day.Count > 0)
                {
                    CreatDay();
                }
            }
        }

        [Category("ternence"), Description("时间段")]
        public List<List<string>> Time
        {
            get { return _Time; }
            set
            {
                _Time = value;
                if (_Time.Count > 0)
                {
                    CreatTime();
                }
            }
        }

        [Category("ternence"), Description("门市价")]
        public string[] Price
        {
            get { return _Price; }
            set
            {
                _Price = value;
                if (_Price == null) return;
                if (_Price.Length > 0)
                {
                    CreatPrice();
                }
            }
        }

        [Category("ternence"), Description("线上价")]
        public string[] OnLienPrice
        {
            get { return _OnLienPrice; }
            set
            {
                _OnLienPrice = value;
                if (_OnLienPrice == null) return;
                if (_OnLienPrice.Length > 0)
                {
                    CreatOnlinePrice();
                }
            }
        }

        [Category("ternence"), Description("返回新日期")]
        public List<string> ResultDay { get; set; }

        [Category("ternence"), Description("包间类型ID")]
        public int TypeId { get; set; }

        [Category("ternence"), Description("时间ID")]
        public List<int> TimeId { get; set; }

        [Category("ternence"), Description("价格ID")]
        public List<int> PricId { get; set; }

        TableLayoutPanel tlp = null;
        TableLayoutPanel pnl = null;
        /// <summary>
        /// 生成表格
        /// </summary>
        private void CreatTable()
        {
            tlp = new TableLayoutPanel();
            tlp.ColumnCount = _ColCount;
            for (int i = 0; i < _ColCount; i++)
            {
                if (i == 1)
                    tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 150F));
            }
            tlp.RowCount = _RowCount;
            for (int i = 0; i < _RowCount; i++)
            {
                tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50F));
            }
            this.Height = _RowCount * 50;
            tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlp.Dock = DockStyle.Fill;
            this.Controls.Add(tlp);
        }

        /// <summary>
        /// 生成覆盖
        /// </summary>
        private void CreatPnl()
        {
            pnl = new TableLayoutPanel();
            pnl.ColumnCount = 1;
            tlp.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 100F));

            pnl.RowCount = 1;
            tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));

            pnl.Dock = DockStyle.Fill;
            pnl.Margin = new System.Windows.Forms.Padding(0, 0, 0, 10);
        }

        /// <summary>
        /// 包间类型写入
        /// </summary>
        private void InsertRoomType()
        {
            CreatPnl(); ;

            Label lbl = new Label();
            lbl.Text = _RoomType;
            lbl.Font = new Font(@"微软雅黑", 12);
            lbl.ForeColor = Color.FromArgb(90, 90, 90);
            lbl.AutoSize = true;
            lbl.Anchor = System.Windows.Forms.AnchorStyles.None;
            pnl.Controls.Add(lbl);

            tlp.SetRowSpan(pnl, RowCount);
            tlp.Controls.Add(pnl, 0, 0);
        }


        /// <summary>
        /// 日期写入
        /// </summary>
        private void CreatDay()
        {
            int c = 0;
            foreach (var dc in _Day)
            {
                CreatPnl();
                Label lbl = new Label();
                for (int i = 0; i < dc.Count; i++)
                    lbl.Text += dc[i] + ",";
                lbl.Text = lbl.Text.Remove(lbl.Text.Length - 1, 1);
                lbl.Font = new Font(@"微软雅黑", 12);
                lbl.ForeColor = Color.FromArgb(90, 90, 90);
                lbl.AutoSize = true;
                lbl.Anchor = System.Windows.Forms.AnchorStyles.None;
                lbl.Click += lblDay_Click;
                pnl.Controls.Add(lbl);

                tlp.SetRowSpan(pnl, RowCount / _Day.Count);
                tlp.Controls.Add(pnl, 1, c * RowCount / _Day.Count);
                c++;
            }

        }


        /// <summary>
        /// 创建时间
        /// </summary>
        private void CreatTime()
        {
            int r = 0;
            foreach (var ti in _Time)
            {
                Label lbl = new Label();

                for (int i = 0; i < ti.Count; i++)
                    lbl.Text = ti[i];

                lbl.Font = new Font(@"微软雅黑", 12);
                
                lbl.ForeColor = Color.FromArgb(90, 90, 90);
                lbl.AutoSize = true;
                lbl.Anchor = System.Windows.Forms.AnchorStyles.None;

                tlp.Controls.Add(lbl, 2, r);
                r++;
            }

        }

        /// <summary>
        /// 创建门市价
        /// </summary>
        private void CreatPrice()
        {
            for (int i = 0; i < _Price.Length; i++)
            {
                TextBox txt = new TextBox();
                txt.Text = _Price[i];
                txt.Font = new Font(@"微软雅黑", 12);
                txt.ForeColor = Color.FromArgb(90, 90, 90);
                txt.TextAlign = HorizontalAlignment.Center;
                txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
                txt.Anchor = System.Windows.Forms.AnchorStyles.None;

                tlp.Controls.Add(txt, 3, i);
            }
        }

        /// <summary>
        /// 创建门市价
        /// </summary>
        private void CreatOnlinePrice()
        {
            for (int i = 0; i < _OnLienPrice.Length; i++)
            {
                TextBox txt = new TextBox();
                txt.Text = _OnLienPrice[i];
                txt.Font = new Font(@"微软雅黑", 12);
                txt.ForeColor = Color.FromArgb(90, 90, 90);
                txt.TextAlign = HorizontalAlignment.Center;
                txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
                txt.Anchor = System.Windows.Forms.AnchorStyles.None;

                tlp.Controls.Add(txt, 4, i);
            }
        }

        /// <summary>
        /// 创建保存
        /// </summary>
        private void CreatSave()
        {
            CreatPnl(); ;

            Label lbl = new Label();
            lbl.Text = "√ 保存";
            lbl.Font = new Font(@"微软雅黑", 12);
            lbl.ForeColor = Color.FromArgb(90, 90, 90);
            lbl.AutoSize = true;
            lbl.Anchor = System.Windows.Forms.AnchorStyles.None;
            lbl.Click += lbl_Click;
            pnl.Controls.Add(lbl);

            tlp.SetRowSpan(pnl, RowCount);
            tlp.Controls.Add(pnl, 5, 0);
        }

        public delegate void SaveEventHandl(object sender, EventArgs e);
        public event SaveEventHandl SaveEvent;
        /// <summary>
        /// 保存事件
        /// </summary>
        void lbl_Click(object sender, EventArgs e)
        {
            if (SaveEvent != null)
                SaveEvent(this, e);
        }


        /// <summary>
        /// 日期修改事件
        /// </summary>
        void lblDay_Click(object sender, EventArgs e)
        {
            Label lblDay = sender as Label;
            ContextMenuStrip cms = new ContextMenuStrip();

            string[] day = lblDay.Text.Split(',');
            Week.ToList<string>().ForEach(c =>
            {
                ToolStripMenuItem tmi = new ToolStripMenuItem();
                day.ToList<string>().ForEach(x =>
                {
                    tmi.Text = c;
                    if (c == x)
                        tmi.Checked = true;
                });
                tmi.Click += tmi_Click;
                cms.Items.Add(tmi);
            });

            cms.Closing += cms_Closing;
            cms.MouseLeave += cms_MouseLeave;
            cms.Show(lblDay, lblDay.Width, 0);

            //纪录原始日期
            OldeWeek = lblDay.Text;
        }



        /// <summary>
        /// 选择日期
        /// </summary>
        void tmi_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem tmi = sender as ToolStripMenuItem;
            tmi.Checked = tmi.Checked ? false : true;
            if (!tmi.Checked)
            {
                //Day.ForEach(c =>
                //{
                //    if (c == tmi.Text)
                //    {
                //        Day.Remove(c);
                //    }
                //});
            }
        }



        /// <summary>
        /// 鼠标离开控件隐藏控件
        /// </summary>
        void cms_MouseLeave(object sender, EventArgs e)
        {
            IsClose = false;
            ContextMenuStrip cms = sender as ContextMenuStrip;
            cms.Close();
            IsClose = true;
        }

        public delegate void DayChengeEventHandl(object sender, EventArgs e);
        public event DayChengeEventHandl DayChengeEvent;
        /// <summary>
        /// 关闭菜单，取消
        /// </summary>
        void cms_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            e.Cancel = IsClose;

            if (!IsClose)
            {
                ContextMenuStrip cms = sender as ContextMenuStrip;

                List<string> NewWeek = new List<string>();

                bool ischange = false;

                //_Day.ToList<string>().ForEach(c =>
                //{
                //    if (c != OldeWeek)
                //    {
                //        NewWeek.Add(c);
                //        ischange = true;
                //    }
                //});
                if (!ischange)
                    return;


                string strs = "";
                for (int i = 0; i < cms.Items.Count; i++)
                {
                    ToolStripMenuItem it = cms.Items[i] as ToolStripMenuItem;
                    if (it.Checked)
                    {
                        strs += cms.Items[i].Text + ",";
                        if (NewWeek.Contains(cms.Items[i].Text))
                            NewWeek.Remove(cms.Items[i].Text);
                    }

                }
                if (!string.IsNullOrEmpty(strs))
                {
                    strs = strs.Remove(strs.Length - 1, 1);
                    NewWeek.Add(strs);
                }



                List<string> Nowday = new List<string>();
                NewWeek.ForEach(c =>
                {
                    if (c.Split(',').Length > 0)
                    {
                        c.Split(',').ToList<string>().ForEach(x =>
                        {
                            Nowday.Add(x);
                        });
                    }
                    else
                    {
                        Nowday.Add(c);
                    }
                });
                string noWeek = string.Empty;
                Week.ToList<string>().ForEach(c =>
                {
                    if (!Nowday.Contains(c))
                        noWeek += c + ",";
                });
                if (!string.IsNullOrEmpty(noWeek))
                {
                    noWeek = noWeek.Remove(noWeek.Length - 1, 1);
                    NewWeek.Add(noWeek);

                }

                ResultDay = NewWeek;

                if (DayChengeEvent != null)
                    DayChengeEvent(this, e);
            }
        }

    }
}
